

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>packnet_sfm.utils.image &mdash; PackNet-SfM 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../configs/configs.html">Configs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../scripts/scripts.html">Scripts</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../trainers/trainers.html">Trainers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../datasets/datasets.html">Datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../models/models.html">Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../networks/networks.html">Networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../losses/losses.html">Losses</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../loggers/loggers.html">Loggers</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../geometry/geometry.html">Geometry</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../utils/utils.html">Utils</a></li>
</ul>
<p class="caption"><span class="caption-text">Contact</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://tri.global">Toyota Research Institute</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/packnet-sfm">PackNet-SfM GitHub</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/TRI-ML/DDAD">DDAD GitHub</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">PackNet-SfM</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>packnet_sfm.utils.image</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for packnet_sfm.utils.image</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020 Toyota Research Institute.  All rights reserved.</span>

<span class="kn">import</span> <span class="nn">torch</span>
<span class="kn">import</span> <span class="nn">torch.nn.functional</span> <span class="k">as</span> <span class="nn">funct</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">lru_cache</span>
<span class="kn">from</span> <span class="nn">PIL</span> <span class="kn">import</span> <span class="n">Image</span>

<span class="kn">from</span> <span class="nn">packnet_sfm.utils.misc</span> <span class="kn">import</span> <span class="n">same_shape</span>

<span class="c1">########################################################################################################################</span>

<div class="viewcode-block" id="load_image"><a class="viewcode-back" href="../../../utils/utils.image.html#packnet_sfm.utils.image.load_image">[docs]</a><span class="k">def</span> <span class="nf">load_image</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Read an image using PIL</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    path : str</span>
<span class="sd">        Path to the image</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    image : PIL.Image</span>
<span class="sd">        Loaded image</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">Image</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">path</span><span class="p">)</span></div>

<span class="c1">########################################################################################################################</span>

<div class="viewcode-block" id="flip_lr"><a class="viewcode-back" href="../../../utils/utils.image.html#packnet_sfm.utils.image.flip_lr">[docs]</a><span class="k">def</span> <span class="nf">flip_lr</span><span class="p">(</span><span class="n">image</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Flip image horizontally</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    image : torch.Tensor [B,3,H,W]</span>
<span class="sd">        Image to be flipped</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    image_flipped : torch.Tensor [B,3,H,W]</span>
<span class="sd">        Flipped image</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">assert</span> <span class="n">image</span><span class="o">.</span><span class="n">dim</span><span class="p">()</span> <span class="o">==</span> <span class="mi">4</span><span class="p">,</span> <span class="s1">&#39;You need to provide a [B,C,H,W] image to flip&#39;</span>
    <span class="k">return</span> <span class="n">torch</span><span class="o">.</span><span class="n">flip</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="p">])</span></div>

<div class="viewcode-block" id="flip_model"><a class="viewcode-back" href="../../../utils/utils.image.html#packnet_sfm.utils.image.flip_model">[docs]</a><span class="k">def</span> <span class="nf">flip_model</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">image</span><span class="p">,</span> <span class="n">flip</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Flip input image and flip output inverse depth map</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model : nn.Module</span>
<span class="sd">        Module to be used</span>
<span class="sd">    image : torch.Tensor [B,3,H,W]</span>
<span class="sd">        Input image</span>
<span class="sd">    flip : bool</span>
<span class="sd">        True if the flip is happening</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    inv_depths : list of torch.Tensor [B,1,H,W]</span>
<span class="sd">        List of predicted inverse depth maps</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">flip</span><span class="p">:</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">flip_lr</span><span class="p">(</span><span class="n">inv_depth</span><span class="p">)</span> <span class="k">for</span> <span class="n">inv_depth</span> <span class="ow">in</span> <span class="n">model</span><span class="p">(</span><span class="n">flip_lr</span><span class="p">(</span><span class="n">image</span><span class="p">))]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">model</span><span class="p">(</span><span class="n">image</span><span class="p">)</span></div>

<span class="c1">########################################################################################################################</span>

<div class="viewcode-block" id="gradient_x"><a class="viewcode-back" href="../../../utils/utils.image.html#packnet_sfm.utils.image.gradient_x">[docs]</a><span class="k">def</span> <span class="nf">gradient_x</span><span class="p">(</span><span class="n">image</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Calculates the gradient of an image in the x dimension</span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    image : torch.Tensor [B,3,H,W]</span>
<span class="sd">        Input image</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    gradient_x : torch.Tensor [B,3,H,W-1]</span>
<span class="sd">        Gradient of image with respect to x</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">image</span><span class="p">[:,</span> <span class="p">:,</span> <span class="p">:,</span> <span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">image</span><span class="p">[:,</span> <span class="p">:,</span> <span class="p">:,</span> <span class="mi">1</span><span class="p">:]</span></div>

<div class="viewcode-block" id="gradient_y"><a class="viewcode-back" href="../../../utils/utils.image.html#packnet_sfm.utils.image.gradient_y">[docs]</a><span class="k">def</span> <span class="nf">gradient_y</span><span class="p">(</span><span class="n">image</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Calculates the gradient of an image in the y dimension</span>
<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    image : torch.Tensor [B,3,H,W]</span>
<span class="sd">        Input image</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    gradient_y : torch.Tensor [B,3,H-1,W]</span>
<span class="sd">        Gradient of image with respect to y</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">image</span><span class="p">[:,</span> <span class="p">:,</span> <span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="p">:]</span> <span class="o">-</span> <span class="n">image</span><span class="p">[:,</span> <span class="p">:,</span> <span class="mi">1</span><span class="p">:,</span> <span class="p">:]</span></div>

<span class="c1">########################################################################################################################</span>

<div class="viewcode-block" id="interpolate_image"><a class="viewcode-back" href="../../../utils/utils.image.html#packnet_sfm.utils.image.interpolate_image">[docs]</a><span class="k">def</span> <span class="nf">interpolate_image</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">shape</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;bilinear&#39;</span><span class="p">,</span> <span class="n">align_corners</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Interpolate an image to a different resolution</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    image : torch.Tensor [B,?,h,w]</span>
<span class="sd">        Image to be interpolated</span>
<span class="sd">    shape : tuple (H, W)</span>
<span class="sd">        Output shape</span>
<span class="sd">    mode : str</span>
<span class="sd">        Interpolation mode</span>
<span class="sd">    align_corners : bool</span>
<span class="sd">        True if corners will be aligned after interpolation</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    image : torch.Tensor [B,?,H,W]</span>
<span class="sd">        Interpolated image</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># Take last two dimensions as shape</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">shape</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">shape</span> <span class="o">=</span> <span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">:]</span>
    <span class="c1"># If the shapes are the same, do nothing</span>
    <span class="k">if</span> <span class="n">same_shape</span><span class="p">(</span><span class="n">image</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">:],</span> <span class="n">shape</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">image</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># Interpolate image to match the shape</span>
        <span class="k">return</span> <span class="n">funct</span><span class="o">.</span><span class="n">interpolate</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">shape</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="n">mode</span><span class="p">,</span>
                                 <span class="n">align_corners</span><span class="o">=</span><span class="n">align_corners</span><span class="p">)</span></div>

<div class="viewcode-block" id="interpolate_scales"><a class="viewcode-back" href="../../../utils/utils.image.html#packnet_sfm.utils.image.interpolate_scales">[docs]</a><span class="k">def</span> <span class="nf">interpolate_scales</span><span class="p">(</span><span class="n">images</span><span class="p">,</span> <span class="n">shape</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;bilinear&#39;</span><span class="p">,</span> <span class="n">align_corners</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Interpolate list of images to the same shape</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    images : list of torch.Tensor [B,?,?,?]</span>
<span class="sd">        Images to be interpolated, with different resolutions</span>
<span class="sd">    shape : tuple (H, W)</span>
<span class="sd">        Output shape</span>
<span class="sd">    mode : str</span>
<span class="sd">        Interpolation mode</span>
<span class="sd">    align_corners : bool</span>
<span class="sd">        True if corners will be aligned after interpolation</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    images : list of torch.Tensor [B,?,H,W]</span>
<span class="sd">        Interpolated images, with the same resolution</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># If no shape is provided, interpolate to highest resolution</span>
    <span class="k">if</span> <span class="n">shape</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">shape</span> <span class="o">=</span> <span class="n">images</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span>
    <span class="c1"># Take last two dimensions as shape</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">shape</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">shape</span> <span class="o">=</span> <span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">:]</span>
    <span class="c1"># Interpolate all images</span>
    <span class="k">return</span> <span class="p">[</span><span class="n">funct</span><span class="o">.</span><span class="n">interpolate</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">shape</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="n">mode</span><span class="p">,</span>
                              <span class="n">align_corners</span><span class="o">=</span><span class="n">align_corners</span><span class="p">)</span> <span class="k">for</span> <span class="n">image</span> <span class="ow">in</span> <span class="n">images</span><span class="p">]</span></div>

<div class="viewcode-block" id="match_scales"><a class="viewcode-back" href="../../../utils/utils.image.html#packnet_sfm.utils.image.match_scales">[docs]</a><span class="k">def</span> <span class="nf">match_scales</span><span class="p">(</span><span class="n">image</span><span class="p">,</span> <span class="n">targets</span><span class="p">,</span> <span class="n">num_scales</span><span class="p">,</span>
                 <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;bilinear&#39;</span><span class="p">,</span> <span class="n">align_corners</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Interpolate one image to produce a list of images with the same shape as targets</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    image : torch.Tensor [B,?,h,w]</span>
<span class="sd">        Input image</span>
<span class="sd">    targets : list of torch.Tensor [B,?,?,?]</span>
<span class="sd">        Tensors with the target resolutions</span>
<span class="sd">    num_scales : int</span>
<span class="sd">        Number of considered scales</span>
<span class="sd">    mode : str</span>
<span class="sd">        Interpolation mode</span>
<span class="sd">    align_corners : bool</span>
<span class="sd">        True if corners will be aligned after interpolation</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    images : list of torch.Tensor [B,?,?,?]</span>
<span class="sd">        List of images with the same resolutions as targets</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># For all scales</span>
    <span class="n">images</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">image_shape</span> <span class="o">=</span> <span class="n">image</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">:]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_scales</span><span class="p">):</span>
        <span class="n">target_shape</span> <span class="o">=</span> <span class="n">targets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span>
        <span class="c1"># If image shape is equal to target shape</span>
        <span class="k">if</span> <span class="n">same_shape</span><span class="p">(</span><span class="n">image_shape</span><span class="p">,</span> <span class="n">target_shape</span><span class="p">):</span>
            <span class="n">images</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Otherwise, interpolate</span>
            <span class="n">images</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">interpolate_image</span><span class="p">(</span>
                <span class="n">image</span><span class="p">,</span> <span class="n">target_shape</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="n">mode</span><span class="p">,</span> <span class="n">align_corners</span><span class="o">=</span><span class="n">align_corners</span><span class="p">))</span>
    <span class="c1"># Return scaled images</span>
    <span class="k">return</span> <span class="n">images</span></div>

<span class="c1">########################################################################################################################</span>

<div class="viewcode-block" id="meshgrid"><a class="viewcode-back" href="../../../utils/utils.image.html#packnet_sfm.utils.image.meshgrid">[docs]</a><span class="nd">@lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">meshgrid</span><span class="p">(</span><span class="n">B</span><span class="p">,</span> <span class="n">H</span><span class="p">,</span> <span class="n">W</span><span class="p">,</span> <span class="n">dtype</span><span class="p">,</span> <span class="n">device</span><span class="p">,</span> <span class="n">normalized</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create meshgrid with a specific resolution</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    B : int</span>
<span class="sd">        Batch size</span>
<span class="sd">    H : int</span>
<span class="sd">        Height size</span>
<span class="sd">    W : int</span>
<span class="sd">        Width size</span>
<span class="sd">    dtype : torch.dtype</span>
<span class="sd">        Meshgrid type</span>
<span class="sd">    device : torch.device</span>
<span class="sd">        Meshgrid device</span>
<span class="sd">    normalized : bool</span>
<span class="sd">        True if grid is normalized between -1 and 1</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    xs : torch.Tensor [B,1,W]</span>
<span class="sd">        Meshgrid in dimension x</span>
<span class="sd">    ys : torch.Tensor [B,H,1]</span>
<span class="sd">        Meshgrid in dimension y</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">normalized</span><span class="p">:</span>
        <span class="n">xs</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">W</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span><span class="p">)</span>
        <span class="n">ys</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">H</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">xs</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">W</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">W</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span><span class="p">)</span>
        <span class="n">ys</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">H</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">H</span><span class="p">,</span> <span class="n">device</span><span class="o">=</span><span class="n">device</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span><span class="p">)</span>
    <span class="n">ys</span><span class="p">,</span> <span class="n">xs</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">meshgrid</span><span class="p">([</span><span class="n">ys</span><span class="p">,</span> <span class="n">xs</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">xs</span><span class="o">.</span><span class="n">repeat</span><span class="p">([</span><span class="n">B</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">]),</span> <span class="n">ys</span><span class="o">.</span><span class="n">repeat</span><span class="p">([</span><span class="n">B</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">])</span></div>

<div class="viewcode-block" id="image_grid"><a class="viewcode-back" href="../../../utils/utils.image.html#packnet_sfm.utils.image.image_grid">[docs]</a><span class="nd">@lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">image_grid</span><span class="p">(</span><span class="n">B</span><span class="p">,</span> <span class="n">H</span><span class="p">,</span> <span class="n">W</span><span class="p">,</span> <span class="n">dtype</span><span class="p">,</span> <span class="n">device</span><span class="p">,</span> <span class="n">normalized</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create an image grid with a specific resolution</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    B : int</span>
<span class="sd">        Batch size</span>
<span class="sd">    H : int</span>
<span class="sd">        Height size</span>
<span class="sd">    W : int</span>
<span class="sd">        Width size</span>
<span class="sd">    dtype : torch.dtype</span>
<span class="sd">        Meshgrid type</span>
<span class="sd">    device : torch.device</span>
<span class="sd">        Meshgrid device</span>
<span class="sd">    normalized : bool</span>
<span class="sd">        True if grid is normalized between -1 and 1</span>

<span class="sd">    Returns</span>
<span class="sd">    -------</span>
<span class="sd">    grid : torch.Tensor [B,3,H,W]</span>
<span class="sd">        Image grid containing a meshgrid in x, y and 1</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">xs</span><span class="p">,</span> <span class="n">ys</span> <span class="o">=</span> <span class="n">meshgrid</span><span class="p">(</span><span class="n">B</span><span class="p">,</span> <span class="n">H</span><span class="p">,</span> <span class="n">W</span><span class="p">,</span> <span class="n">dtype</span><span class="p">,</span> <span class="n">device</span><span class="p">,</span> <span class="n">normalized</span><span class="o">=</span><span class="n">normalized</span><span class="p">)</span>
    <span class="n">ones</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">ones_like</span><span class="p">(</span><span class="n">xs</span><span class="p">)</span>
    <span class="n">grid</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">stack</span><span class="p">([</span><span class="n">xs</span><span class="p">,</span> <span class="n">ys</span><span class="p">,</span> <span class="n">ones</span><span class="p">],</span> <span class="n">dim</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">grid</span></div>

<span class="c1">########################################################################################################################</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Toyota Research Institute (TRI)

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(false);
      });
  </script>

  
  
    
   

</body>
</html>